<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2005 – </title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2005 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2005">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2 id="useless-echo-instead-of-echo-cmd-just-use-cmd">Useless
<code>echo</code>? Instead of <code>echo $(cmd)</code>, just use
<code>cmd</code></h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2005.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="bu">echo</span> <span class="st">&quot;</span><span class="va">$(</span><span class="fu">whoami</span><span class="va">)</span><span class="st">&quot;</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2005.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="fu">whoami</span></span></code></pre></div>
<h3 id="rationale">Rationale</h3>
<p>ShellCheck found the unnecessary construct
<code>echo "$(somecommand here)"</code>.</p>
<p>This is generally due to a misunderstanding about what
<code>echo</code> does. It has no role in "showing on screen" or
similar, but simply writes a string to standard output. This is also how
all other programs output data.</p>
<p><code>echo "$(somecommand)"</code> will capture the output
<code>somecommand</code> writes to standard output and write it to
standard output, where it was already going. At best this is a no-op,
but it may have several other negative effects:</p>
<ul>
<li>It disables parallel processing in pipelines, such as
<code>echo "$(find . -name '*.iso')" | xargs sha1sum</code> which does
not allow iterating files and checksumming at the same time. Similarly,
users don't see incremental updates as programs run.</li>
<li>It introduces shell and echo related pitfalls like being unable to
output the string <code>-n</code>, stripping NUL bytes and trailing
linefeeds, and expanding escape sequences in some shells but not
others.</li>
<li>It suppresses the exit code of the command, so that
<code>echo "$(grep '^user:' /etc/passwd)"</code> no longer returns with
failure when the user is not found.</li>
<li>It does not allow programs to tailor their output for terminals,
such as <code>ls</code> vs <code>echo "$(ls)"</code> where the former
outputs columns and colors according to user preferences, while the
latter doesn't.</li>
<li>It uses unnecessary memory to buffer up the data before writing it
where it was already going.</li>
</ul>
<p>To avoid all this, simply replace <code>echo "$(somecommand)"</code>
with <code>somecommand</code> as in the example. It's shorter, faster,
and more correct.</p>
<h3 id="exceptions">Exceptions</h3>
<p>If you are relying on one of the otherwise detrimental effects for
correctness, you can consider one of:</p>
<pre><code># Suppress exit code without the other negative effects
cmd || true

# Disable tty specific output without the other negative effects
cmd | cat

# Buffer up potentially large output without using more memory or modifying the content in any way
cmd &gt; file.tmp
cat file.tmp

# Exactly like `echo &quot;$(cmd)&quot;`, but allows output like `-n` and works the same across shells
printf &#39;%s\n&#39; &quot;$(cmd)&quot;</code></pre>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


